<template>
  <div class="box">
    <div id="printTest">
      <div
        class="box_printTest"
        v-for="(item, index) in printDataList"
        :key="index"
        style="page-break-after: always"
      >
        <div class="header">
          <div class="header-icon-box">
            <img src="@/assets/common/company_logo.png" />
            <span class="title">广州众山精密科技有限公司</span>
          </div>
          <h2 class="header-title">{{ title }}</h2>
          <div class="header-qrcode" ref="qrCodeUrl"></div>
        </div>
        <div class="container">
          <div class="container-basic-info" v-if="basicConfig">
            <div
              class="basic-info-row"
              v-for="(bItem, idx) in basicConfig"
              :key="idx"
            >
              <div
                class="row-item-modify"
                v-for="(iItem, idxx) in bItem"
                :key="idxx"
              >
                <span class="label">{{ iItem.label }}: </span>
                <span class="value">{{ item[iItem.prop] }}</span>
              </div>
            </div>
          </div>
          <table id="printId" v-if="tableHeaderConfig">
            <thead>
              <tr>
                <td>行号</td>
                <td v-for="(hItem, hIdx) in tableHeaderConfig" :key="hIdx">
                  {{ hItem.label }}
                </td>
              </tr>
            </thead>
            <tbody v-if="item.tableData.length">
              <tr v-for="(sItem, sIdx) in item.tableData" :key="sIdx">
                <td>{{ (sIdx + 1) * 10 }}</td>
                <td v-for="(tdItem, tdIdx) in tableHeaderConfig" :key="tdIdx">
                  {{ sItem[tdItem.prop] }}
                </td>
              </tr>
            </tbody>
          </table>
          <div class="container-order-info">
            <div class="order-info-row" v-if="signConfig">
              <div
                class="row-item-modify"
                v-for="(item, idx) in signConfig"
                :key="idx"
              >
                <span class="label">{{ item.label }}: </span>
                <span class="value">{{ item.value }}</span>
              </div>
            </div>
          </div>
          <div class="container-footer-info" v-if="footerConfig">
            {{ footerConfig.label }}：{{ item[footerConfig.prop] }}
          </div>
        </div>
      </div>
    </div>
    <div></div>
  </div>
</template>

<script>
import printJS from "print-js";
import QRCode from "qrcodejs2";
export default {
  props: {
    printDataList: {
      type: Array,
      default: () => [],
    },
    qrCodeProp: {
      type: String,
      default: "qrCode",
    },
    title: {
      type: String,
      default: "成品入库单",
    },
    basicConfig: {
      type: Array,
      default: () => [],
    },
    tableHeaderConfig: {
      type: Array,
      default: () => [],
    },
    signConfig: {
      type: Array,
      default: () => [],
    },
    footerConfig: {
      type: Object,
      default: () => {},
    },
  },
  methods: {
    async printHandle() {
      let flag = await this.creatQrCode();
      if (flag) {
        printJS({
          printable: "printTest",
          type: "html",
          targetStyles: ["*"],
        });
      }
    },
    creatQrCode() {
      return new Promise((resolve) => {
        const qrcodeURL = this.$refs.qrCodeUrl;

        Promise.all(
          qrcodeURL.map((item, index) => {
            return new Promise((qrResolve) => {
              new QRCode(item, {
                text: this.printDataList[index][this.qrCodeProp], // 需要转换为二维码的内容
                width: 96,
                height: 96,
                colorDark: "#000",
                colorLight: "#ffffff",
                // correctLevel: QRCode.CorrectLevel.H,
              });
              qrResolve(true);
            });
          })
        ).then(() => {
          resolve(true);
        });
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.box {
  background: white;
}
.box_printTest {
  margin-left: 30px;
  width: 1000px;
  .header {
    display: flex;
    flex-shrink: 0;
    justify-content: space-between;
    align-items: center;
    padding-bottom: 32px;
    border-bottom: 1px solid black;
    .header-icon-box {
      display: flex;
      .icon {
      }
      .title {
        line-height: 48px;
        font-size: 20px;
        font-weight: 500;
        color: #181b92;
      }
      img {
        display: inline-block;
        width: 48px;
        height: 48px;
      }
    }
    .header-title {
      flex: 1;
      text-align: center;
      // margin-left: -10px;
      font-weight: 600;
      font-size: 24px;
      transform: scale(2);
      width: 0;
    }
    .header-qrcode {
      display: block;
      width: 96px;
      height: 96px;
      // transform: scale(2);
    }
  }
  .container {
    width: 100%;
    .container-basic-info {
      padding: 28px 0 40px;
      .basic-info-row {
        display: flex;
        justify-content: space-between;
        flex-wrap: wrap;
        // align-items: left;
        padding: 12px 0;
        .row-item-modify {
          flex: 1;
          font-size: 18px;
          display: flex;
          flex-wrap: nowrap;
          min-width: 1px;
          .label {
            // padding-right: 5px;
          }
          .value {
            flex: 1;
            overflow: hidden;
          }
          .min-label {
            min-width: 46px;
          }
          .max-label {
            min-width: 90px;
          }
        }
        .row-item-textarea {
          flex: 1;
          font-size: 18px;
          display: flex;
          .max-label {
            min-width: 90px;
          }
        }
      }
    }

    table {
      border: 1px solid black;
      width: 100%;
      vertical-align: middle;
      thead {
        background: #f3f3f3;
        td {
          min-width: 90px;
          height: 56px;
        }
      }
      tbody {
        td {
          padding: 16px;
          border-right: 1px solid black;
        }
      }
      td {
        min-height: 56px;
        padding-left: 16px;
        text-align: left;
        border-right: 1px solid black;
        border-bottom: 1px solid black;
      }
    }
    .container-order-info {
      display: block;
      margin: 24px 0 0;
      min-height: 18px;
      .order-info-row {
        display: flex;
        .row-item-modify {
          flex: 1;
        }
      }
    }
    .container-footer-info {
      padding: 40px 0 0;
      font-weight: 500;
      font-size: 18px;
    }
  }
}

/* 去掉页眉页脚 */
@page {
  size: auto;
  margin: 0mm;
}
</style>
